#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：15.自定义类完成文件读写.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-10-30 17:00
'''
class MyopenFile:
    def __init__(self, file_path):
        self.file_obj = None
        self.file_path = file_path

    def __enter__(self):
        print("即将创建文件对象")
        self.file_obj = open(self.file_path, encoding="utf-8", mode="r")
        return self


    def __exit__(self, exc_type, exc_val, exc_tb):
        print("即将释放文件对象")
        self.file_obj.close()

    def my_read(self):
        print(self.file_obj.read())


with MyopenFile("测试文件.txt") as file:
    file.my_read()